package com.ruoyi.buiness.service.impl;

import java.util.List;

import com.ruoyi.buiness.domain.Phone;
import com.ruoyi.buiness.mapper.PhoneMapper;
import com.ruoyi.buiness.service.IPhoneService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * phoneService业务层处理
 *
 * @author ruoyi
 * @date 2020-12-19
 */
@Service
public class PhoneServiceImpl implements IPhoneService {
    @Autowired
    private PhoneMapper phoneMapper;

    /**
     * 查询phone
     *
     * @param id phoneID
     * @return phone
     */
    @Override
    public Phone selectPhoneById(Long id) {
        return phoneMapper.selectPhoneById(id);
    }

    /**
     * 查询phone列表
     *
     * @param phone phone
     * @return phone
     */
    @Override
    public List<Phone> selectPhoneList(Phone phone) {
        return phoneMapper.selectPhoneList(phone);
    }

    /**
     * 新增phone
     *
     * @param phone phone
     * @return 结果
     */
    @Override
    public int insertPhone(Phone phone) {
        return phoneMapper.insertPhone(phone);
    }

    /**
     * 修改phone
     *
     * @param phone phone
     * @return 结果
     */
    @Override
    public int updatePhone(Phone phone) {
        return phoneMapper.updatePhone(phone);
    }

    /**
     * 删除phone对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePhoneByIds(String ids) {
        return phoneMapper.deletePhoneByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除phone信息
     *
     * @param id phoneID
     * @return 结果
     */
    @Override
    public int deletePhoneById(Long id) {
        return phoneMapper.deletePhoneById(id);
    }


    /**
     * 导入通讯录数据
     *
     * @param dataList 通讯录列表
     * @return 结果
     */
    @Override
    public String importPhone(List<Phone> dataList) {
        if (StringUtils.isNull(dataList) || dataList.size() == 0) {
            throw new BusinessException("导入通讯录数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Phone phone : dataList) {
            this.insertPhone(phone);
            successNum++;
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
